package piwan;

public class Vector3 {
	public double x, y, z;
	
	Vector3( double ix, double iy, double iz ) { x = ix; y = iy; z = iz; }
	Vector3( double[] ic ) { x = ic[0]; y = ic[1]; z = ic[2]; }
	Vector3() { x = y = z = 0.0; }
	
	public void	set( Vector3 v ) { x=v.x;y=v.y;z=v.z; }
	public Vector3	add( Vector3 v ) { x+=v.x; y+=v.y; z+=v.z; return this; }
	public Vector3	sub( Vector3 v ) { x-=v.x; y-=v.y; z-=v.z; return this; }
	public Vector3	mul( double s ) { x*=s; y*=s; z*=s; return this; }
	public Vector3	plus( Vector3 v ) { return new Vector3(x+v.x,y+v.y,z+v.z); }
	public Vector3	minus( Vector3 v ) { return new Vector3(x-v.x,y-v.y,z-v.z); }
	public Vector3	times( double s ) { return new Vector3(x*s,y*s,z*s); }
	
	public double	dist2(Vector3 v) { return minus(v).norm2(); }
	public double	dist(Vector3 v) { return Math.sqrt(dist2(v)); }
	public boolean	closerThan(Vector3 v, double thr) { return dist2(v)<=thr*thr; }
	public double	dot( Vector3 v ) { return x*v.x + y*v.y + z*v.z; }
	public double	norm2() { return dot(this); }
	public double	norm() { return Math.sqrt(norm2()); }
	public void set( double newX, double newY, double newZ ) { x = newX; y = newY; z = newZ; }
	
	public Vector3 clone() { return new Vector3(x,y,z); }
	
	public Vector3	unit() {
		double n = norm();
		if( n == 0.0 )
			return new Vector3(0.0,0.0,0.0);
		else
			return new Vector3(x/n,y/n,z/n);
	}
}